package com.qixiucoding.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author longhj
 * @Date 2021/11/20 星期六 0:16
 */
@Slf4j
public class JsonResourceUtils {


    public JsonResourceUtils() {

    }


    public static JSONObject getJsonObjFromConfiguration(){

        String FILENAME = "configuration.json";
        return getJsonObjFromResource(FILENAME);
    }

    public static JSONObject getJsonObjFromResource(String filename){

        JSONObject json = null;

        if (!filename.contains(".json")){

            filename += ".json";
        }

        try{

            // InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
            System.out.println(getPropertyFilePath());
            InputStream inputStream = new FileInputStream(JsonResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath());
            if (inputStream != null){

                StringBuilder sb = new StringBuilder();
                InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
                BufferedReader bfReader = new BufferedReader(reader);
                String content = null;
                while((content = bfReader.readLine()) != null){

                    sb.append(content);
                }
                bfReader.close();
                json = JSON.parseObject(sb.toString());
            }else{

                log.info("[{}] file not exist",filename);
            }

        }catch (Exception e){

            log.error("read file to string error",e);
        }

        return json;
    }

    public static String[] parseJsonArray(JSONObject json,String arg){

        JSONArray jsonArray = (JSONArray)json.get(arg);
        List<String> list = (List<String>)JSONArray.parseArray(jsonArray.toString(),String.class);
        return list.toArray(new String[list.size()]);
    }
    /**
     * 得到jar配置文件路径
     */
    public static String getPropertyFilePath() throws IOException {
        String jarWholePath = JsonResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getFile();

        jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8");

        String jarPath = new File(jarWholePath).getParentFile().getAbsolutePath() + "/";
        return jarPath;
    }

    // public static void main(String[] args) {
    //     System.out.println(System.getProperty("user.dir"));
    //     String pathname = "config/config.json";
    //     File file = new File(pathname);
    //     try (InputStream inputStream = new FileInputStream(file)) {
    //
    //     } catch (FileNotFoundException e) {
    //         e.printStackTrace();
    //     } catch (IOException e) {
    //         e.printStackTrace();
    //     }
    //     // try {
    //     //     BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
    //     //     System.out.println(bufferedReader.readLine());
    //     // } catch (IOException e) {
    //     //     e.printStackTrace();
    //     // }
    //     // try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileSystemResource(pathname).getInputStream())){
    //     //
    //     // } catch (IOException e) {
    //     //     e.printStackTrace();
    //     // }
    //     // // ResourceLoaderAware
    //     // System.out.println("file.exists() = " + file.exists());
    //     // JSONObject json =  getJsonObjFromConfiguration();
    //     // MyJson myJson = JSONObject.toJavaObject(json, MyJson.class);
    //     // System.out.println(myJson);
    //     // Integer resultCode =  (Integer) json.get("你好");
    //     // System.out.println(resultCode);
    //     //
    //     // String[] strings = parseJsonArray(json,"tags");
    //     // for (String string:strings){
    //     //
    //     //     System.out.print(string+" ");
    //     // }
    // }
}
